﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Model.CSP;
using CSP.Bll;
using CSP.Common.Tools;
using System.Text;

namespace CSP.Web.UserControls
{
    public partial class SynthesizeUserCredit : CSPUserControlBase
    {
        private int _serviceCredit = 0;

        private int _consumeCredit = 0;

        //服务能力
        private double _serviceAbility = 0;

        private double _serviceAttitude = 0;

        private bool _isShowServiceCredit = true;

        private bool _isShowConsumeCredit = true;

        private bool _isServiceAbility = true;

        private bool _isServiceAttitude = true;

        private int _creditCount = 0;

        private string _userName;

        private string SYNTHESIZEUSERCREDITHTMLKEY = "SynthesizeUserCreditHtmlKey";

        /// <summary>
        /// 当前用户控件生成的HMTL
        /// </summary>
        private string SynthesizeUserCreditHtml
        {
            get {
                if (ViewState[SYNTHESIZEUSERCREDITHTMLKEY] == null)
                {
                    return "";
                }
                else {
                    return ViewState[SYNTHESIZEUSERCREDITHTMLKEY].ToString();
                }
            }

            set{
                ViewState[SYNTHESIZEUSERCREDITHTMLKEY] = value;
            }
        }

        private CSPLayout _layout;

        /// <summary>
        /// 是否显示服务信誉度
        /// </summary>
        public bool IsShowServiceCredit
        {
            get { return _isShowServiceCredit; }
            set { _isShowServiceCredit = value; }
        }


        /// <summary>
        /// 是否显示消费信誉度
        /// </summary>
        public bool IsShowConsumeCredit
        {
            get { return _isShowConsumeCredit; }
            set { _isShowConsumeCredit = value; }
        }


        /// <summary>
        /// 是否显示服务能力
        /// </summary>
        public bool IsServiceAbility
        {
            get { return _isServiceAbility; }
            set { _isServiceAbility = value; }
        }


        /// <summary>
        /// 是否显示服务态度
        /// </summary>
        public bool IsServiceAttitude
        {
            get { return _isServiceAttitude; }
            set { _isServiceAttitude = value; }
        }

        /// <summary>
        /// 布局
        /// </summary>
        public CSPLayout LayoutStyle
        {
            get { return _layout; }
            set { _layout = value; }
        }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }

        /// <summary>
        /// 服务信誉度
        /// </summary>
        protected int ServiceCredit
        {
            get { return _serviceCredit; }
        }

        /// <summary>
        /// 消费信誉度
        /// </summary>
        protected int ConsumeCredit
        {
            get { return _consumeCredit; }
        }

        /// <summary>
        /// 服务能力
        /// </summary>
        protected double ServiceAbility
        {
            get { return _serviceAbility; }
        }

        /// <summary>
        /// 服务态度
        /// </summary>
        protected double ServiceAttitude
        {
            get { return _serviceAttitude; }
        }

        /// <summary>
        /// 服务基数
        /// </summary>
        protected int CreditCount
        {
            get
            {
                return _creditCount;
            }
        }




        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                SetUserCredit();
            }
            CreatePageHtml();

        }

        /// <summary>
        /// 设置用户信誉
        /// </summary>
        private void SetUserCredit()
        {
            UserCredit userCredit = GetUserCredit();
            if (userCredit != null)
            {
                _serviceCredit = userCredit.ServiceCredit;
                _consumeCredit = userCredit.ConsumeCredit;
                _serviceAbility = GetRatio(userCredit.CreditCount, userCredit.ServiceAbility);
                _serviceAttitude = GetRatio(userCredit.CreditCount, userCredit.ServiceAttitude);
                _creditCount = userCredit.CreditCount;
                
            }

            //this.DIV_Credit.Attributes.Add("class", GetLayoutStyle());

            //this.P_ServiceCredit.Visible = IsShowServiceCredit;
            //if (ServiceCredit == 0)
            //{
            //    this.Img_ServiceCredit.Visible = false;
            //    this.LB_ServiceCreditCode.InnerHtml = ServiceCredit.ToString();
            //}
            //else
            //{
            //    this.Img_ServiceCredit.Src = "/images/evaluate/" + GetServiceCreditGrade(ServiceCredit);
            //    this.Img_ServiceCredit.Attributes.Add("title", ServiceCredit.ToString());
            //}

            //this.P_ConsumeCredit.Visible = IsShowConsumeCredit;
            //if (ConsumeCredit == 0)
            //{
            //    this.Img_ConsumeCredit.Visible = false;
            //    this.LB_ConsumeCreditCode.InnerHtml = ConsumeCredit.ToString();
            //}
            //else
            //{
            //    this.Img_ConsumeCredit.Src = "/images/evaluate/" + GetConsumptionCreditGrade(ConsumeCredit);
            //    this.Img_ServiceCredit.Attributes.Add("title", ConsumeCredit.ToString());
            //}

            //this.P_ServiceAttitude.Visible = IsServiceAttitude;
            //this.Img_ServiceAttitude.Attributes.Add("class", "ui-star ui-star-lv " + (Convert.ToInt32(ServiceAttitude)).ToString());
            //this.LB_ServiceAttitudeCode.InnerHtml = (ServiceAttitude).ToString("0.0");

            //this.P_ServiceAbility.Visible = IsServiceAbility;
            //this.Img_ServiceAbility.Attributes.Add("class", "ui-star ui-star-lv " + (Convert.ToInt32(ServiceAbility)).ToString());
            //this.LB_ServiceAbilityCode.InnerHtml = (ServiceAbility).ToString("0.0");

            //LB_FeedbackRate.InnerHtml = (Convert.ToDecimal(ServiceCredit) / (CreditCount == 0 ? 1 : Convert.ToDecimal(CreditCount) * 100)).ToString("0.00");
         

        }

        private void CreatePageHtml()
        {
            
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(SynthesizeUserCreditHtml))
            {
                this.LB_SynthesizeUserCredit.Text = SynthesizeUserCreditHtml;
                return;
            }
            sb.Append("<div id='DIV_Credit'  class='" + GetLayoutStyle() + "' style=''>");
            sb.Append("<p style='display: " + GetCreditStatus(IsShowServiceCredit) + "' id='P_ServiceCredit' >");
            if (ServiceCredit<4)
            {
                sb.Append("服务信誉：" + ServiceCredit.ToString());
            }
            else{
                sb.Append("服务信誉：<img align='absmiddle' border='0' class='rank' title='" + ServiceCredit .ToString()+ "' id='Img_ServiceCredit'  src='/images/evaluate/" + GetServiceCreditGrade(ServiceCredit) + "'>");
            }
            sb.Append("</p>");
           
            sb.Append("<p style='display: " + GetCreditStatus(IsShowConsumeCredit) + "' id='P_ConsumeCredit'  >");
            if (ConsumeCredit < 4)
            {
                sb.Append("消费信誉：" + ConsumeCredit.ToString());
            }
            else
            {
                sb.Append("消费信誉：<img align='absmiddle' border='0' class='rank' title='" + ConsumeCredit.ToString() + "' id='Img_ConsumeCredit' src='/images/evaluate/" + GetConsumptionCreditGrade(ConsumeCredit) + "'>");
            }

            sb.Append("</p>");
            sb.Append("<p style='display: " + GetCreditStatus(IsServiceAttitude) + "' id='P_ServiceAttitude' >");
            sb.Append("服务态度：<span class='ui-star ui-star-lv" + (Convert.ToInt32(ServiceAttitude)).ToString() + "' id='Img_ServiceAttitude' ></span><span");
            sb.Append("  class='score'><i><span  id='LB_ServiceAttitudeCode'>" + (ServiceAttitude).ToString("0.0") + "</span></i>分</span></p>");
            sb.Append("<p style='display: " + GetCreditStatus(IsServiceAbility) + "' >");
            sb.Append("执教能力：<span class='ui-star ui-star-lv" + (Convert.ToInt32(ServiceAbility)).ToString() + " '></span><span ");
            sb.Append("  class='score'><i><span  id='Span1'>" + (ServiceAbility).ToString("0.0") + "</span></i>分</span></p>");
            sb.Append("<div class='sever-info'>");
            sb.Append("<span>好 评 率：<span  id='LB_FeedbackRate'></span>" + (Convert.ToDecimal(ServiceCredit) / (CreditCount == 0 ? 1 : Convert.ToDecimal(CreditCount) * 100)).ToString("0.00") + "%</span></div>");
            sb.Append("</div>");
            SynthesizeUserCreditHtml = sb.ToString();
            this.LB_SynthesizeUserCredit.Text = sb.ToString();
        }


        /// <summary>
        /// 获取用户信誉度对象
        /// </summary>
        /// <returns></returns>
        private UserCredit GetUserCredit()
        {
            UserCredit result = null;
            using (var userCreditBll = CSPBllFactory.GetInstance().GetUserCreditBll())
            {
                result = userCreditBll.GetByUser(_userName);
            }

            return result;
        }

        /// <summary>
        /// 获取比率 公式=totalScore/creditCount
        /// </summary>
        /// <param name="creditCount">信誉评价基数</param>
        /// <param name="totalScore">总得分</param>
        /// <returns></returns>
        private double GetRatio(int creditCount, int totalScore)
        {
            double result = 0;
            if (creditCount == 0)
            {
                return result;
            }
            result = creditCount == 0 ? 0 : (Convert.ToDouble(totalScore) / Convert.ToDouble(creditCount));
            return result;

        }

        /// <summary>
        /// 获取服务等级
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        protected string GetServiceCreditGrade(int score)
        {
            return CommonTool.GetServiceCreditGradeByScore(score);
        }

        /// <summary>
        /// 获取消费等级
        /// </summary>
        /// <param name="score"></param>
        /// <returns></returns>
        protected string GetConsumptionCreditGrade(int score)
        {
            return CommonTool.GetConsumptionCreditGradeByScore(score);
        }

        /// <summary>
        /// 获取信誉状态 是否显示
        /// </summary>
        /// <param name="isShow">是否显示</param>
        /// <returns></returns>
        protected string GetCreditStatus(bool isShow)
        {
            if (isShow)
            {
                return "block";
            }
            else
            {
                return "none";
            }
        }

        /// <summary>
        /// 获取布局样式
        /// </summary>
        /// <returns></returns>
        protected string GetLayoutStyle()
        {
            string result = "ui-credit";
            if (_layout == CSPLayout.Horizontal)
            {
                result = "inline-style";
            }

            return result;
        }

        public enum CSPLayout
        {
            /// <summary>
            /// 纵
            /// </summary>
            Vertical = 0,

            /// <summary>
            /// 横
            /// </summary>
            Horizontal = 1
        }
    }
}